using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.ComponentModel;

namespace TanashCMS.Business
{
    public class ProductOption : CMSDataObject
    {
        private string mValue;
        private ProductOptionType mOptionType = null;
        private int mProductID=0;

        public virtual string Value { get { return mValue; } set { mValue = value; } }
        public virtual ProductOptionType OptionType { get { return mOptionType; } set { mOptionType = value; } }
        public virtual int ProductID { get { return mProductID; } set { mProductID = value; } }

        public static List<ProductOption> List(int ProductID)
        {
            return (List<ProductOption>)Configuration.GetCurrentSession().CreateQuery("select po from ProductOption po where po.ProductID = " + ProductID.ToString() + " order by po.OptionType.ID").List<ProductOption>();
        }

        public static List<ProductOptionType> ListOptionTypes(int WebsiteID)
        {
            return (List<ProductOptionType>)Configuration.GetCurrentSession().CreateQuery("select pot from ProductOptionType pot where pot.Website.ID = " + WebsiteID.ToString() + " order by pot.Title").List<ProductOptionType>();
        }

        public static void Update(ProductOption option)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                session.SaveOrUpdate(option);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }

        public static void Insert(ProductOption option)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                session.Save(option);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }

        public static void Delete(ProductOption option)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                session.Delete(option);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }
    }
}
